﻿using System.Data.Objects.SqlClient;
using System.Linq;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.InsuranceProducts;

namespace ZBJF.Warranty.BLL
{
    public partial class InsuranceProductPackageService
    {
        public InsuranceProductPackage LoadByID(long id, bool isAvailable = true)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceProductPackage>();
            if (isAvailable)
            {
                expressionWhere = expressionWhere.And(o => o.ID == id);
            }
            else
            {
                expressionWhere = expressionWhere.And(o => o.IsAvailable == isAvailable);
            }
            return LoadEntities(expressionWhere).FirstOrDefault();
        }
        public bool IsExist(string productPackageName, int id = 0)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceProductPackage>().And(o => o.ProductPackageName.Equals(productPackageName));
            if (id > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ID == id);
            }
            return LoadEntities(expressionWhere).Any();

        }

        /// <summary>
        /// 禁用
        /// </summary>
        /// <param name="id"></param>
        /// <param name="available"></param>
        /// <returns></returns>
        public bool UpdateAvailable(int id, bool available = false)
        {
            var entity = LoadByID(id, true);
            if (entity != null)
            {
                entity.IsAvailable = available;
                return UpdateEntities(entity);
            }
            return false;
        }

        public IQueryable<InsuranceProductPackage> GetPagingList(InsuranceProductPackageModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceProductPackage>();
            if (!string.IsNullOrEmpty(model.Keyword))
            {
                expressionWhere = expressionWhere.And(o => o.ProductPackageName.Contains(model.Keyword) || o.InsuranceProductPackageNO.Equals(model.Keyword));
            }
            if (!string.IsNullOrEmpty(model.ProductPackageName))
            {
                expressionWhere = expressionWhere.And(o => o.ProductPackageName.Contains(model.ProductPackageName));
            }
            if (model.IsAvailable.HasValue)
            {
                expressionWhere = expressionWhere.And(o => o.IsAvailable == model.IsAvailable);
            }
            if (model.CreateTimeStart.HasValue)
            {
                expressionWhere = expressionWhere.And(o => o.CreateTime >= model.CreateTimeStart);
            }
            if (model.CreateTimeEnd.HasValue)
            {
                expressionWhere = expressionWhere.And(o => o.CreateTime < SqlFunctions.DateAdd("dd", 1, model.CreateTimeEnd));
            }
            if (string.IsNullOrEmpty(model.iSorts))
            {
                model.iSorts = " CreateTime asc";
            }
            return LoadPagerEntities<InsuranceProductPackage>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
    }
}